<?php

/**
* alltosun.com 资讯widget article.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-3 下午2:45:09 $
* $Id$
*/

class article_widget
{
    private $per_page = 10;
    /**
     * 获取资讯 正常列表
     */
    public function get_list($param = array())
    {
        $page_no = Request::getParam('page_no', 1);
        if (isset($param['per_page']) && $param['per_page']) {
            $this->per_page = $param['per_page'];
        }
        if (isset($param['category_id']) && $param['category_id']) {
            $filter['category_id'] = $param['category_id'];
        }
        if (isset($param['category_title']) && $param['category_title']) {
            $category_filter = array(
                    'status'        => 1,
                    'res_name'      => 'article',
                    '`title` LIKE ' => '%'.$param['category_title'].'%',
                    'parent_id'     => 0
            );
            $filter['category_id'] = _uri('category', $category_filter, 'id');
        }
        if (isset($param['inherit']) && $param['inherit']) {
            $category_id = $filter['category_id'];
            $filter['category_id'] = category_helper::get_children_ids($category_id);
            array_unshift($filter['category_id'], $category_id);
        }
        $filter['status'] = 1;

        $list = array();
        $total = _model('article')->getTotal($filter);
        if ($total) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($total)) {
                Response::assign('pager', $pager);
            }
            $list = _model('article')->getList($filter, ' ORDER BY `ovder_view` ASC '.$pager->getLimit($page_no));
        }

        return $list;
    }

    /**
     * 获取资讯 无限加载列表
     */
    public function get_article_list($param = array())
    {
        $page_no = Request::getParam('page_no', 1);
        $category_id = 0 ;

        if (isset($param['per_page']) && $param['per_page']) {
            $this->per_page = $param['per_page'];
        }
        if (isset($param['page_no']) && $param['page_no']) {
            $page_no = $param['page_no'];
        }
        if (isset($param['category_id']) && $param['category_id']) {
            $filter['category_id'] = $param['category_id'];
            $category_id = $filter['category_id'];
        }
        if (isset($param['inherit']) && $param['inherit']) {
            if ($category_id == 0) {
                $filter['category_id'] = _model('category')->getFields('id', array('status'=>1));
            } else {
                $filter['category_id'] = category_helper::get_children_ids($category_id);
                array_unshift($filter['category_id'], $category_id);
            }
        }
        $filter['status'] = 1;

        $order = ' ORDER BY `id` DESC ';
        $limit = ' LIMIT '.($page_no-1)*$this->per_page.','.$this->per_page;

        $list = _model('article')->getList($filter, $order.$limit);
        $new_list = array();
        foreach($list as $k=>$v) {
            if ($v['cover'] == '') {
                unset($list[$k]);
            } else {
                $v['cover'] = _image($v['cover']);
                $new_list[] = $v;
            }
        }

        return $new_list;
    }

    /**
     * 是否有更多的数据
     */
    public function has_more($param = array())
    {
        $page_no        = Request::getParam('page_no', 1);
        $this->per_page = Request::getParam('per_page', 1);
        if (isset($param['per_page']) && $param['per_page']) {
            $this->per_page = $param['per_page'];
        }
        if (isset($param['page_no']) && $param['page_no']) {
            $page_no = $param['page_no'];
        }
        if (isset($param['category_id']) && $param['category_id']) {
            $filter['category_id'] = $param['category_id'];
        }
        $filter['status'] = 1;

        $total = _model('article')->getTotal($filter);

        $more_num = 0;
        if ($total > $page_no*$this->per_page) {
            $more_num = 1;
        }

        return $more_num;
    }
}
?>